package cn.wolfcode.wolf2w.advice;

import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CommonExceptionHandler {

    /**
     * 拦截关于LogicException.class类型的异常
     * 返回自定义错误对象,JsonResult.error() 传入状态码 提示信息 数据(null)
     * @param e
     * @return
     */
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object logicExc(Exception e) {
        e.printStackTrace();
        return JsonResult.error(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
    }

    /**
     * RuntimeException.class类型的异常   系统异常
     * 返回默认错误对象,JsonResult.defaultError()
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object RuntimeExc(Exception e) {
        e.printStackTrace();
        return JsonResult.defaultError();
    }
}
